﻿namespace TradePublish.Service.impl
{
    using TradePublish.Common;
    using log4net;
    using TradePublish.Common;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Threading;

    internal class BingManager
    {
        private ILog log = null;

        public BingManager()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        private List<string> findInfoUrls(string html)
        {
            if ((html == null) || (html.Length == 0))
            {
                return null;
            }
            if (html.IndexOf("以下搜索结果包含") > 0)
            {
                return null;
            }
            int index = html.IndexOf("class=\"sa_wr\"");
            if (index < 0)
            {
                return null;
            }
            List<string> list = new List<string>();
            int startIndex = html.IndexOf("<a ", index);
            string item = this.trueUrl(html.Substring(startIndex, html.IndexOf(">", startIndex) - startIndex));
            if (item != null)
            {
                list.Add(item);
            }
            int num3 = html.IndexOf("class=\"sa_wr\"", (int) (index + 10));
            if (num3 > 0)
            {
                int num4 = html.IndexOf("<a ", num3);
                string str2 = this.trueUrl(html.Substring(num4, html.IndexOf(">", num4) - num4));
                if (str2 != null)
                {
                    list.Add(str2);
                }
                int num5 = html.IndexOf("class=\"sa_wr\"", (int) (num3 + 10));
                if (num5 > 0)
                {
                    int num6 = html.IndexOf("<a ", num5);
                    string str3 = this.trueUrl(html.Substring(num6, html.IndexOf(">", num6) - num6));
                    if (str3 != null)
                    {
                        list.Add(str3);
                    }
                }
            }
            return list;
        }

        private string graspBingHTML(string keyword)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("q", keyword);
            parameters.Add("qs", "bs");
            parameters.Add("form", "QBLH");
            parameters.Add("first", "1");
            string response = null;
            int num = 0;
            while (true)
            {
                try
                {
                    response = new GetData().GetResponse("http://www.bing.com.cn/search", parameters);
                    if (response == null)
                    {
                        throw new Exception("%%%%%%访问Bing，返回null ");
                    }
                    return response;
                }
                catch (Exception exception)
                {
                    num++;
                    Thread.Sleep(0x3e8);
                    this.log.Error(string.Concat(new object[] { "=====第", num, "次访问Bing发生异常.keyword=", keyword, exception.Message }));
                    if (num > 2)
                    {
                        return null;
                    }
                }
            }
        }

        public List<string> graspUrl(string keyword)
        {
            string html = this.graspBingHTML(keyword);
            if (html == null)
            {
                return null;
            }
            List<string> list = null;
            try
            {
                list = this.findInfoUrls(html);
            }
            catch (Exception)
            {
                list = null;
                this.log.Error("不该发生的异常：keyword=" + keyword);
            }
            return list;
        }

        private string trueUrl(string aHtml)
        {
            aHtml = aHtml.Substring(aHtml.IndexOf("href=\"") + "href=\"".Length);
            string url = aHtml.Substring(0, aHtml.IndexOf("\""));
            if (Utils.isTopDomain(url))
            {
                return url;
            }
            return null;
        }
    }
}

